﻿var p;

setItemPageTitle();

function setItemPageTitle(){
    var s = localStorage.getItem("currentP");
    p = JSON.parse(s);
    $("title").html(p.raw_title+"_歌瑞森");
}

// 1.确定页面内容
setItemPageContent();

function setItemPageContent(){
    // mainpic.src = p.pic_url
    $("#mainpic").attr("src",p.pic_url);
    $("#ptitle").html(p.title);
    $("#pprice").html(p.view_price);
    $(".fee").html( "运费:"+p.view_fee);
    $(".sale").html(p.view_sales);
    $(".loc").html(p.item_loc);
}

//2.详情
window.onload=function(){
	
var detail_images = document.getElementById("detail_images");
    $.get("http://test.hfjava.com/pro/product_detail_info?nid= " + p.nid,{},function (data) {

            console.log(data);
            
            var detailImages = data.detailDesc.newWapDescJson;
            console.log(detailImages);
            for (var i = 0; i < detailImages.length; i++) {
                if (detailImages[i].data.length > 5) {
                    for (var j = 0; j < detailImages[i].data.length; j++) {
                        var imgs1 = document.createElement("img");
                        imgs1.src = detailImages[i].data[j].img;
                        imgs1.style.width = "100%";

                        detail_images.appendChild(imgs1);
                    }
                }
            }
        });

}

// 3.加入购物车
function addShopCar(){
    var product = {
        title:p.title,
        price:p.view_price,
        count:"1",
        pic:p.pic_url,
        nid:p.nid,
    }

    var shopCar = JSON.parse(localStorage.getItem("shopCar"));

    if(!shopCar){
        var shopCar = [];
        shopCar.push(product);
    }else{
        var isFind = false; 
        for(var i=0;i<shopCar.length;i++){
            if(shopCar[i].nid == p.nid){
                shopCar[i].count++;
                isFind = true;
                break;
            }
        }

        if(!isFind){
            shopCar.push(product);
        }
    }

localStorage.setItem("shopCar",JSON.stringify(shopCar));

     	 $.toast("加入成功！");
    
}
